<?php

/**
 * 香港接口
 * 短信服务
 * author:censhuixing
 * time:2014-10-21
 */
class xgate
{

	var $config = array('url'=>'http://smsc.xgate.com.hk/smshub/sendsms',
	'username'=>'wljiashi',
	'password'=>'wljs1017',
	'sign_message'=>'【幸福之都】',//签名前置
	);

	function xgate()
	{

	}
	/*
	@param $mobile 要发送的手机号码(群发半角逗号,隔开)
	@param $msg 发送内容*/
	function send_sms($msg='',$mobile='')
	{
		$msg = urlencode($msg.$this->config['sign_message']);
		//$msg = $msg.$this->config['sign_message'];
		$fields =array(
		'UserID'=>$this->config['username'],
		'UserPassword'=>$this->config['password'],
		'MessageType'=>'TEXT',
		'MessageReceiver'=>'852'.$mobile,
		'MessageLanguage'=>'UTF8',
		'MessageBody'=>$msg
		);
		$xml = $this->post_curl($this->config['url'],$fields);
	    var_dump($xml);die();
		$xml_obj =  simplexml_load_string($xml);
		$xml_arr =  $this->object2array($xml_obj);

		if($xml_arr['Success'] == true )
		{
			return true;
		}

		else
		{
			$this->log($xml_arr);//失败写入日志
			return false;
		}


	}

	function object2array($object)
	{
		return @json_decode(@json_encode($object),1);
	}

	function post_curl($url, $fields)
	{
		$fields_string ='';
		foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&' ; }
		rtrim($fields_string ,'&');
		$ch = curl_init();
		// 设置 curl 相应属性
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
		$returnTransfer = curl_exec($ch);
		curl_close($ch);
		return $returnTransfer;
	}

	function log($arr = array())
	{
		if(empty($arr))
		return;
		
		$arr['time']  = date('Y-m-d H:i:s');//加上时间记录
		$content = "\r";
		$content .= "\$".time()."= " . var_export($arr, true) . ";\r\n";
		$dir =   date('Y-m-d');
		$file = ROOT_PATH.'/log/sendsms/'.$dir.'.log';//绝对路径
		file_put_contents($file,$content,FILE_APPEND);

	}



	//测试手机
	function send_test_mobile($content='',$mobile='')
	{
		if(empty($content) OR empty($mobile))return;
		$ret = $this->send_sms($content,$mobile);
		//$ret = $this->return_str($ret);
		return $ret;
	}


}


?>